{{ $x := . }}
{{ if ($.IsHome) }}{{ $x = (site) }}{{ end }}

{{ $description := (site.Params.description) }}
{{ with (.Summary) }}{{ $description = . }}{{ end }}
{{ with (.Description) }}{{ $description = . }}{{ end }}

{{ $pages := (slice) }}
{{ if (or ($.IsHome) ($.IsSection)) }}
    {{ $pages = (where ($x.RegularPages) "Section" "in" (site.Params.content.sections)) }}
{{ else }}
    {{ $pages = ($x.Pages) }}
{{ end }}

{{ $limit := (site.Config.Services.RSS.Limit) }}
{{ if (ge ($limit) 1) }}{{ $pages = (($pages) | first ($limit)) }}{{ end }}

{{ ((printf "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>") | safeHTML) }}
<rss version="2.0"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
  <channel>
      {{ if (eq (.Title) (site.Title)) }}
        <title>{{ (site.Title) }}</title>
      {{ else }}
        <title>{{ with (.Title) }}{{ . }} / {{ end }}{{ (site.Title) }}</title>
      {{ end }}
    <link>{{ (.Permalink) }}</link>
    <description>{{ ($description) }}</description>
      {{ with (site.LanguageCode) }}
        <language>{{ . }}</language>
      {{ end }}
    <image>
      <link>{{ (.Permalink) }}</link>
      <url>{{ ("favicon-128.png" | absURL) }}</url>
        {{ if (eq (.Title) (site.Title)) }}
          <title>{{ (site.Title) }}</title>
        {{ else }}
          <title>{{ with (.Title) }}{{ . }} / {{ end }}{{ (site.Title) }}</title>
        {{ end }}
    </image>
      {{ if (not (.Date.IsZero)) }}
        <lastBuildDate>{{ (.Date.UTC.Format "Mon, 02 Jan 2006 15:04:05 -0700") }}</lastBuildDate>
      {{ end }}
      {{ with (.OutputFormats.Get "RSS") }}
          {{ ((printf "<atom:link href=%q rel='self' type=%q/>" (.Permalink) (.MediaType)) | safeHTML) }}
      {{ end }}
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
      {{ range ($pages) }}
        <item>
          <title>{{ (.Title) }}</title>
          <link>{{ (.Permalink) }}</link>
          <description>{{ ((.Summary) | plainify) }}</description>
          <pubDate>{{ (.Date.UTC.Format "Mon, 02 Jan 2006 15:04:05 -0700") }}</pubDate>
            {{ range (.Params.authors) }}
                {{ with (site.GetPage "taxonomyTerm" (printf "authors/%s" (urlize .))) }}
                    {{ if (.Params.email) }}
                      <author>{{ (.Params.email) }} ({{ (.Title) }})</author>
                    {{ else }}
                      <dc:creator>{{ (.Title) }}</dc:creator>
                    {{ end }}
                {{ end }}
            {{ end }}
            {{ if .Params.categories }}
                {{ range (.GetTerms "categories") }}
                  <category>{{ (.Title) }}</category>
                {{ end }}
            {{ else }}
                {{ range (.GetTerms "tags") }}
                  <category>{{ ((.Title) | lower) }}</category>
                {{ end }}
            {{ end }}
          <guid>{{ (.Permalink) }}</guid>
        </item>
      {{ end }}
  </channel>
</rss>
